import { get } from 'lodash-es';
import { point, distance } from '@turf/turf';
import Convertor from '@gis/lib/utils/convertor.js';

export function getLngLat(data, next) {
  let lng, lat;
  if (typeof data === 'string' && data.indexOf(',') > 0) {
    data = data.split(',');
  }
  if (Array.isArray(data)) {
    lng = data[0];
    lat = data[1];
  } else if (data && typeof data === 'object') {
    if (data.lng && data.lat) {
      lng = data.lng;
      lat = data.lat;
    } else if (data.longitude && data.latitude) {
      lng = data.longitude;
      lat = data.latitude;
    }
  }
  if (lng && lat) {
    typeof next === 'function' && next([lng, lat]);
    return [lng, lat];
  }
  return null;
}

export function getWGS84LngLat(data) {
  const lngLat = getLngLat(data);
  if (!lngLat) {
    return null;
  }
  const [longitude, latitude] = Convertor.bd2wgs(lngLat.longitude, lngLat.latitude);
  return { longitude, latitude };
}

export function getPointBCR(data, options) {
  let width = get(data, 'width') || get(options, 'width');
  let height = get(data, 'height') || get(options, 'height');
  let offset = get(data, 'offset') || get(options, 'offset');
  let anchor = get(data, 'anchor') || get(options, 'anchor');
  width = Number.isFinite(width) && width > 0 ? width : 46;
  height = Number.isFinite(height) && height > 0 ? height : 64;
  if (!offset) {
    let halfH = height / 2;
    let yEnum = { bottom: -1 * halfH, top: halfH, topLabel: -1 * height, bottomLabel: height };
    offset = [0, yEnum[anchor] || 0];
  }
  return { width, height, offset };
}

export function getPointTitle(data) {
  return data ? data.text || data.name || data.label || data.title : '';
}

export function getGeoPointData(poi, defaultData) {
  if (!poi || !poi.properties) return defaultData;
  return poi.properties.originData || defaultData;
}

export function createGeoPoint(data, options) {
  /* data: { icon, width, height, lng, lat // 这些是必要的 } */
  options = Object.assign({ anchor: 'bottom' }, options);
  let { width, height, offset } = getPointBCR(data, options);
  let coordinates = getLngLat(data);
  let getTitle = options.getTitle;
  if (typeof getTitle !== 'function') {
    getTitle = getPointTitle;
  }
  if (!coordinates) {
    return null;
  }
  let geoData = {
    geometry: { type: 'Point', coordinates: coordinates },
    properties: {
      width: width,
      height: height,
      text: getTitle(data),
      offset: offset,
      originData: data,
      cluster: false,
      enablePicked: true, // 是否可以拾取
      autoSelect: false, // 根据鼠标位置来自动设置选中项
    },
  };
  if (data.icon) {
    geoData.properties.icon = data.icon;
  }
  return geoData;
}

export function createGeoPoints(dataList, options) {
  if (!Array.isArray(dataList) || !dataList.length) return [];
  return dataList.reduce((res, item) => {
    item = createGeoPoint(item, options);
    if (item) res.push(item);
    return res;
  }, []);
}

export function createIcon(icon, width = 46, height = 64) {
  if (icon instanceof BMap.Icon) {
    return icon;
  }
  return new BMap.Icon(icon, new BMap.Size(width, height), {
    anchor: new BMap.Size(width / 2, height * 0.9),
    infoWindowAnchor: new BMap.Size(width / 2, 0),
  });
}

export function isInCircle(poi, center, radius) {
  poi = getLngLat(poi);
  center = getLngLat(center);
  if (!poi || !center || !Number.isFinite(radius) || radius <= 0) {
    return false;
  }
  let val = distance(point(center), point(poi), { units: 'kilometers' });
  val = Number((val * 1000).toFixed(0));
  // console.log('distance: ', val, radius);
  return Number.isFinite(val) ? 0 <= val && val <= radius : false;
}
